package com.rpc.example.spring.customer;

import com.rpc.example.RegistoryFacory;
import com.rpc.example.constant.RegisterType;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

public class RpcReferenceBean implements FactoryBean<Object> {

    private String applicationName;

    private Object object;
    private String serviceAddress;
    private int port;
    private Class<?> clazz;

    private String registerAddress;

    private byte registryType;


    @Override
    public Object getObject() throws Exception {
        return this.object;
    }

    @Override
    public Class<?> getObjectType() {
        return this.clazz;
    }

    public void init() {
        this.object = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new InvokeProxy(
                RegistoryFacory.createRegisterService(this.registerAddress, RegisterType.findByCode(this.registryType)), applicationName)
        );
    }


    public String getServiceAddress() {
        return serviceAddress;
    }

    public void setServiceAddress(String serviceAddress) {
        this.serviceAddress = serviceAddress;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public void setClazz(Class<?> clazz) {
        this.clazz = clazz;
    }

    public String getRegisterAddress() {
        return registerAddress;
    }

    public void setRegisterAddress(String registerAddress) {
        this.registerAddress = registerAddress;
    }

    public byte getRegistryType() {
        return registryType;
    }

    public void setRegistryType(byte registryType) {
        this.registryType = registryType;
    }

    public String getApplicationName() {
        return applicationName;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }
}


